package cn.pzhuweb.gpa.service.impl;

import cn.pzhuweb.gpa.dao.StudentDao;
import cn.pzhuweb.gpa.pojo.Student;
import cn.pzhuweb.gpa.utils.HttpClientConfig;
import cn.pzhuweb.gpa.utils.MySqlTool;
import cn.pzhuweb.gpa.view.MainView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author KK
 * @CreateTime 2022-08-29
 */
public class MainServiceImpl {

    public static List<Student> go(String param,String cookie) throws SQLException {
        List<Student> students = new ArrayList<>();
        //获取用户姓名和学号
        Document user = Jsoup.parse(HttpClientConfig.doGet(cookie));
        String stuId = user.selectFirst("#account").val();
        String name = user.selectFirst("#realName").val();

        Document document = Jsoup.parse(HttpClientConfig.doPOST(param, cookie));
        Elements tr = document.getElementsByTag("tr");
        for (int i = 1; i < tr.size(); i++) {
            int c = i+1;
            String html = document.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(9)").html();
            String subject = document.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(4)").html();
            if (!html.equals("0") || !subject.equals("计算机基础1")) {
                String time = document.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(2)").html();
                String credit = document.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(7)").html();
                String grade = document.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(9)").html();
                Student student = new Student(stuId, name, subject, credit, grade, time);
                System.out.println(student);
                students.add(student);
            }
        }

        org.jsoup.nodes.Document document2 = Jsoup.parse(HttpClientConfig.doPOST("kksj=2021-2022-2", cookie));
        Elements tr2 = document2.getElementsByTag("tr");
        for (int i = 1; i < tr2.size(); i++) {
            int c = i+1;
            String html = document2.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(9)").html();
            String subject = document2.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(4)").html();
            if (!html.equals("0") || !subject.equals("计算机基础1")) {
                String time = document2.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(2)").html();
                String credit = document2.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(7)").html();
                String grade = document2.selectFirst("#dataList > tbody > tr:nth-child(" + c + ") > td:nth-child(9)").html();
                Student student = new Student(stuId, name, subject, credit, grade, time);
                System.out.println(student);
                students.add(student);
            }
        }
        MySqlTool mySqlTool = new MySqlTool();
        try {
            mySqlTool.insert(students);
        }catch (SQLException e){

        }finally {
            return students;
        }
    }

}
